<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input @clear="getEmployeeList" clearable placeholder="请输入员工姓名" v-model="params.name" class="search-main" />
      <el-button type="primary" @click="getEmployeeList">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="addEmployee">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="delEmployee(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text" @click="reset(scope.row.id)">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination layout="total,sizes, prev, pager, next, jumper" :current-page="params.page"
        :page-sizes="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" :page-size="params.pageSize" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <!-- 添加员工 -->
    <el-dialog
    :title="this.addForm.id ? '编辑员工' : '添加员工'"
    :visible="dialogVisible"
    width="480px"
    @close="closeDialog"
>
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" @click="confirmAdd" type="primary">确 定</el-button>
      </template>
    </el-dialog>
    <!-- 添加员工弹窗 -->

  </div>
</template>

<script>
import { getEmployeeListAPI, createEmployeeAPI, delEmployeeAPI, editEmployeeAPI, resetPasswordAPI, getRoleListAPI} from '@/api/employee'
export default {
  name: 'Employee',
  data() {
    return {
      employeeList: [],//数据列表
      dialogVisible: false, // 控制弹框打开关闭
      roleList: [
        // {
        //   id: 1,
        //   name: '超级管理员'
        // },
        // {
        //   id: 2,
        //   name: '基础数据管理'
        // },
        // {
        //   id: 3,
        //   name: '行车管理'
        // },
        // {
        //   id: 4,
        //   name: '员工及系统管理'
        // },
        // {
        //   id: 5,
        //   name: '黑马测试'
        // }
      ], // 角色列表
      params: {
        page: 1,
        pageSize: 10,
        name: '' // 员工姓名
      },
      total: 1,
      addForm: {
        name: '',
        phonenumber: '',
        roleId: '',
        status: 1,
        userName: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      },
    }
  },
  mounted() {
    this.getEmployeeList()
    this.getdropList()
  },
  methods: {
    //获取列表信息
    async getEmployeeList() {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    //状态
    formatStatus(status) {
      return status === 1 ? '启用' : '禁用'
    }, 
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize//更新每页条数
      this.getEmployeeList()
    },
    handleCurrentChange(page) {//更新页数
      this.params.page = page
      this.getEmployeeList()
    },
    // 关闭弹框
    closeDialog() {
      this.dialogVisible = false
      this.addForm = {
                name: '',
                phonenumber: '',
                roleId: '',
                status: 1,
                userName: ''
              }
    },
    // 打开弹框
    addEmployee() {
      this.dialogVisible = true
    },
    //添加&编辑
    confirmAdd() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.addForm.id) {
            // console.log('编辑')
            editEmployeeAPI(this.addForm).then(() => {
              this.$message.success('修改成功')
              //刷新列表
              this.getEmployeeList()
            }).catch((error) => {
              this.$message.error(error.response.data.msg)
            })
          } else {
            // console.log('添加')
            // 1. 提交接口
            createEmployeeAPI(this.addForm).then(() => {
              this.$message.success('添加成功')
              // 刷新列表
              this.getEmployeeList()
              //清空
            }).catch((error) => {
              this.$message.error(error.response.data.msg)
            })
          }
          // 3. 关闭弹框
          this.closeDialog()
        }
      })
    },
    //删除
    delEmployee(id) {
      this.$confirm("是否确定删除").then(() => {
        delEmployeeAPI(id).then(() => {
          this.getEmployeeList()
          this.$message.success('删除成功')
        }).catch(() => {
          this.$message.error('删除失败')
        })
      })
    },
    //修改
    edit(row) {
      this.addEmployee()
      const {id,name,phonenumber,roleId,status,userName} = row
      this.addForm = {
        id,name,phonenumber,roleId,status,userName
      }
    },
    //重置密码
    reset(id){
      this.$confirm("是否重置密码为'123456'").then(() => {
        resetPasswordAPI(id).then(() => {
        this.$message.success('重置密码成功')
      }).catch(() => {
        this.$message.error('重置失败')
      })
      })
    },
    //获取下拉列表
    getdropList(){
      getRoleListAPI().then((res) => {
        this.roleList = res.data
        // console.log(res.data)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>